<?php
// take care at moment you are defining these constants, 
// make sure you are guiding usign the constants defined 
// in DocumentType.php
define('DOCUMENTTYPE_THESIS', 1);
define('DOCUMENTTYPE_MASTER_THESIS', 2);
define('DOCUMENTTYPE_PHD_ARTICLE', 3);
define('DOCUMENTTYPE_MAGAZINE_ARTICLE', 4);
define('DOCUMENTTYPE_BOOK', 5);
define('DOCUMENTTYPE_ENCYCLOPEDIA', 6);

class Install extends Controller {
	
	public static $documentTypes = array(
		DOCUMENTTYPE_THESIS => 'Tesis de Pregrado',	
		DOCUMENTTYPE_MASTER_THESIS => 'Tesis de Maestria',
		DOCUMENTTYPE_PHD_ARTICLE => 'Tesis de Doctorado',
		DOCUMENTTYPE_MAGAZINE_ARTICLE => 'Articulo de Revista',
		DOCUMENTTYPE_BOOK => 'Libro',
		DOCUMENTTYPE_ENCYCLOPEDIA => 'Enciclopedia'
	);
	
	public function __construct() {
    parent::Controller();
  }

  public function index() {
  	echo "<h3>Welcome to SISMAPC Installation process</h3>";
  	
  	$this->createTables();		
  	$this->createDocumentTypes();
  	$this->createPermissionsAndRoles();
  	$this->createUsers();
  	
  	echo "<p>Intalation process finished successfully</p>";
  }
  
	private function createTables() {
//		echo 'Reminder: Make sure the tables do not exist already.<br />
//		<form action="" method="POST">
//		<input type="submit" name="action" value="Create Tables"><br /><br />';
//		
//		if ($this->input->post('action')) {
			//Doctrine::debug(true);
			//Doctrine_Manager::connection()->execute('SET FOREIGN_KEY_CHECKS = 0');
			Doctrine::createTablesFromModels();
			$arrayLoadedModels = Doctrine::getLoadedModels();
			
			echo "<p>Models loaded:<br/>";
			foreach ($arrayLoadedModels as $model) {
			  echo $model."<br/>";
			}
			echo "</p>";
						
      echo "<p>Tables created successfully!...</p><br/>";
//		}
	}
	
	private function createDocumentTypes() {
		foreach ( Install::$documentTypes as $key => $value ) {
			$dt = new DocumentType();
    	$dt->documentType = $value;
    	$dt->save();	
		}
		
		echo '<p>DocumentTypes created successfully!...</p><br/>';
	}
	
	private function createPermissionsAndRoles() {
		$p1 = new Permission(); 
		$p1->permission = PERM_ADMINISTER_DOCUMENTS;
		$p1->save();
		
		$p2 = new Permission(); 
		$p2->permission = PERM_ADMINISTER_USERS;
		$p2->save();
		
		$p3 = new Permission(); 
		$p3->permission = PERM_ACCESS_DOWNLOAD_DOCUMENTS;
		$p3->save();
		
		$p4 = new Permission(); 
		$p4->permission = PERM_ACCESS_DOCUMENTS;
		$p4->save();
		
		$r1 = new Role();
		$r1->name = ROLE_ADMINISTRATOR;
		$r1->permissions[0] = $p1;
		$r1->permissions[1] = $p2;
		$r1->save();
		
		$r2 = new Role();
		$r2->name = ROLE_ANONYMOUS_USER;
		$r2->permissions[0] = $p4;
		$r2->save();
		
		$r3 = new Role();
		$r3->name = ROLE_AUTHENTICATED_USER;
		$r3->permissions[0] = $p3;
		$r3->save();
		
		echo '<p>Permissions and Roles created successfully!...</p><br/>';
	}
	
	private function createUsers() {
		$rolesTable = Doctrine_Core::getTable('Role');
		$roles = $rolesTable->findAll();
		//$roles = new Doctrine_Collection(Doctrine_Core::getTable('Role'));
		
		$anonymousUser = new User();
		$anonymousUser->username = "";
		$anonymousUser->password = "";
		$anonymousUser->email = "";
		// assign the "usuario anonimo" role
		$anonymousUser->roles[0] = $roles[1];
		$anonymousUser->save();

		$user = new User();
		$user->username = "admin";
		$user->password = md5("123");
		$user->email = "guidoc@suss.co.cu";
		// assign the administrador role
		$user->roles[0] = $roles[0];
		
		$user->save();
		
		echo '<p>Users created successfully!...</p><br/>';
	}
	
	
}